// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.database.policies;

import org.dengues.designer.ui.database.commands.DBNoteCreateCommand;
import org.dengues.designer.ui.database.commands.DBTableCreateCommand;
import org.dengues.designer.ui.database.commands.DiagramChildSetConstraintCommand;
import org.dengues.designer.ui.database.parts.DBNoteEditPart;
import org.dengues.designer.ui.database.parts.DBTableEditPart;
import org.dengues.designer.ui.database.parts.ForeignKeyLabelEditPart;
import org.dengues.model.database.DBNote;
import org.dengues.model.database.DBTable;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.model.database.ForeignKeyLabel;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.XYLayoutEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class DBDiagramLayoutEditPolicy extends XYLayoutEditPolicy {

    /**
     * Qiang.Zhang.Adolf@gmail.com DBDiagramLayoutEditPolicy constructor comment.
     */
    public DBDiagramLayoutEditPolicy() {
    }

    @Override
    protected Command createChangeConstraintCommand(EditPart child, Object constraint) {
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editpolicies.ConstrainedLayoutEditPolicy#createChangeConstraintCommand(org.eclipse.gef.requests.ChangeBoundsRequest,
     * org.eclipse.gef.EditPart, java.lang.Object)
     */
    @Override
    protected Command createChangeConstraintCommand(ChangeBoundsRequest request, EditPart child, Object constraint) {
        if (child instanceof DBTableEditPart && constraint instanceof Rectangle) {
            DBTable model = (DBTable) child.getModel();
            return new DiagramChildSetConstraintCommand(request, model, (Rectangle) constraint);
        } else if (child instanceof DBNoteEditPart && constraint instanceof Rectangle) {
            return new DiagramChildSetConstraintCommand(request, (DBNote) child.getModel(), (Rectangle) constraint);
        } else if (child instanceof ForeignKeyLabelEditPart && constraint instanceof Rectangle) {
            return new DiagramChildSetConstraintCommand(request, (ForeignKeyLabel) child.getModel(), (Rectangle) constraint);
        }
        return super.createChangeConstraintCommand(request, child, constraint);
    }

    @Override
    protected Command getCreateCommand(CreateRequest request) {
        Object childClass = request.getNewObject();
        if (childClass instanceof DBTable) {
            return new DBTableCreateCommand((DatabaseDiagram) getHost().getModel(), request);
        } else if (childClass instanceof DBNote) {
            return new DBNoteCreateCommand((DatabaseDiagram) getHost().getModel(), request);
        }
        return null;
    }
}
